package com.bwie.house.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bwie.house.config.CrawlerTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.house.entity.CrawlerNewhouseEntity;
import com.bwie.house.service.CrawlerNewhouseService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author ÔóÓî
 * @email sunlightcs@gmail.com
 * @date 2022-04-24 11:33:06
 */
@RestController
@RequestMapping("house/crawlernewhouse")
public class CrawlerNewhouseController {
    @Autowired
    private CrawlerNewhouseService crawlerNewhouseService;

    @Autowired
    private CrawlerTest crawlerTest;


    /**
     * crawlerNewhouseService
    * @author 張泽宇
    * @date 2022/4/24 11:49
    * @return
    */
    @RequestMapping("/saveBatch")
    public R saveBatch(){
        //抛异常
        try {
            //调用工具类 获取爬虫   爬到的数据
            List<CrawlerNewhouseEntity> crawlerNewhouseEntities = crawlerTest.CrawlerTest();
            //调用mybadis进行批量添加
            crawlerNewhouseService.saveBatch(crawlerNewhouseEntities);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = crawlerNewhouseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{houseId}")
    public R info(@PathVariable("houseId") Integer houseId){
		CrawlerNewhouseEntity crawlerNewhouse = crawlerNewhouseService.getById(houseId);

        return R.ok().put("crawlerNewhouse", crawlerNewhouse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CrawlerNewhouseEntity crawlerNewhouse){
		crawlerNewhouseService.save(crawlerNewhouse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CrawlerNewhouseEntity crawlerNewhouse){
		crawlerNewhouseService.updateById(crawlerNewhouse);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] houseIds){
		crawlerNewhouseService.removeByIds(Arrays.asList(houseIds));

        return R.ok();
    }

}
